import * as actions from '../actions/index';

// 对页面prop 数据进行管理
export const initialState = {
  SecuritySettingData: [],
  SecurityRecordData: [],
  projects: [],
  groups: [],
  pageSize: 10,
  total: 0,
  currentPage: 1,
  isLoadingTable: true,
  paramsData: null,
};
const defaultAction = {
  type: 'doNothing',
};

export default function index(state = initialState, action = defaultAction) {
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.RECEIVE_SETTING_DATA:
      return { ...state, SecuritySettingData: action.data };
    case actions.SET_DATA:
      return {
        ...state,
        SecuritySettingData: state.SecuritySettingData.map((e) => {
          if (action.data.id === e.id) {
            return {
              ...action.data,
            };
          }
          return e;
        }),
      };
    case actions.RECEIVE_RECORD_DATA: {
      const total = action.data.shift().amount;
      return {
        ...state,
        SecurityRecordData: action.data,
        total,
        currentPage: action.data.currentPage,
      };
    }
    case actions.RECEIVE_PARAMS_DATA:
      return { ...state, paramsData: action.data };
    case actions.LOADING:
      return { ...state, isLoadingTable: action.data };
    case actions.RECEIVE_PROJECT:
      return { ...state, projects: action.data };
    case actions.RECEIVE_ORG_SETTING_DATA:
      return { ...state, isSensitiveBehaviorEnable: action.data };
    case actions.RECEIVE_GROUP:
      return { ...state, groups: action.data };
    default:
      return state;
  }
}
